package com.smarttest.portal.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smarttest.common.beans.CommonConstants;
import com.smarttest.common.beans.JsonDataResult;
import com.smarttest.portal.mapper.RuleMapper;
import com.smarttest.portal.mapper.RuleParamMapper;
import com.smarttest.portal.pojo.Rule;
import com.smarttest.portal.pojo.RuleExample;
import com.smarttest.portal.pojo.RuleParam;
import com.smarttest.portal.pojo.RuleParamExample;
import com.smarttest.portal.pojo.RuleExample.Criteria;
import com.smarttest.portal.service.RuleService;

@Service
public class RuleServiceImpl implements RuleService {
	@Autowired
    private RuleMapper ruleMapper;
	@Autowired
    private RuleParamMapper ruleParamMapper;
	@Override
	public JsonDataResult createRule(String indexName,String ruleName,String ruleType) {
		// TODO Auto-generated method stub
		String ruleData="[{\"group\":\"埋点信息\",\"params\":[{\"k\":\"埋点的key(用逗号分隔)\",\"v\":\"\"}]},{\"group\":\"运算类型\",\"params\":[{\"k\":\"请在avg,sum,max,min中选择\",\"v\":\"\"}]},{\"group\":\"浮动区间(选填)\",\"params\":[{\"k\":\"最小\",\"v\":\"\"},{\"k\":\"最大\",\"v\":\"\"}]},{\"group\":\"报警接收人\",\"params\":[{\"k\":\"叮叮电话(用，号分隔)\",\"v\":\"\"}]},{\"group\":\"前推时间(单位:ms)\",\"params\":[{\"k\":\"从该时间起\",\"v\":\"\"}]}]";
		RuleParamExample example = new RuleParamExample();
		com.smarttest.portal.pojo.RuleParamExample.Criteria criteria = example.createCriteria();
		criteria.andRuleTypeEqualTo(ruleType);
		// TODO Auto-generated method stub
		List<RuleParam> list = ruleParamMapper.selectByExampleWithBLOBs(example);
		//判断是否查询到结果
		if (list != null && list.size() > 0) {
			RuleParam ruleParam = list.get(0);
			ruleData = ruleParam.getParamData();
		}
		
		Rule rule = new Rule();
		rule.setRuleindex(indexName);
        rule.setRulename(ruleName);
		rule.setRuleData(ruleData);
		rule.setOnOff(CommonConstants.SWITCH_ON);
		rule.setCreated(new Date());
		rule.setUpdated(new Date());
		ruleMapper.insert(rule);
		return JsonDataResult.ok();
	}
	
	@Override
	public JsonDataResult getRuleByParameter(String indexName,String ruleName) {
		// TODO Auto-generated method stub
		RuleExample example = new RuleExample();
		Criteria criteria = example.createCriteria();
		if(null!=indexName&&!("".equals(indexName))){
			criteria.andRuleindexEqualTo(indexName);
		}if(null!=ruleName&&!("".equals(ruleName))){
			criteria.andRulenameEqualTo(ruleName);
		}
		// TODO Auto-generated method stub
		List<Rule> list = ruleMapper.selectByExampleWithBLOBs(example);
		//判断是否查询到结果
		if (list != null && list.size() > 0) {
			return JsonDataResult.ok(list);
		}
		return JsonDataResult.ok();
	}

	@Override
	public JsonDataResult updateRule(String indexName, String ruleName, String ruleBody,String modifiedRuleName) {
		// TODO Auto-generated method stub
		RuleExample example = new RuleExample();
		Criteria criteria = example.createCriteria();
		if(null!=indexName&&!("".equals(indexName))){
			criteria.andRuleindexEqualTo(indexName);
		}if(null!=ruleName&&!("".equals(ruleName))){
			criteria.andRulenameEqualTo(ruleName);
		}
		// TODO Auto-generated method stub
		List<Rule> list = ruleMapper.selectByExampleWithBLOBs(example);
		//判断是否查询到结果
		if (list != null && list.size() > 0) {
			Rule rule = list.get(0);
			rule.setRuleData(ruleBody);
			if(null!=modifiedRuleName&&!"".equals(modifiedRuleName)){
				rule.setRulename(modifiedRuleName);
			}
			ruleMapper.updateByPrimaryKeyWithBLOBs(rule);
			return JsonDataResult.ok();
		}else{
			return JsonDataResult.error(300);
		}
	}

	@Override
	public JsonDataResult getValidRuleByParameter(String indexName) {
		// TODO Auto-generated method stub
				RuleExample example = new RuleExample();
				Criteria criteria = example.createCriteria();
				if(null!=indexName&&!("".equals(indexName))){
					criteria.andRuleindexEqualTo(indexName);
				}
				criteria.andOnOffEqualTo(CommonConstants.SWITCH_ON);
				example.setOrderByClause("created asc");
				// TODO Auto-generated method stub
				List<Rule> list = ruleMapper.selectByExampleWithBLOBs(example);
				//判断是否查询到结果
				if (list != null && list.size() > 0) {
					return JsonDataResult.ok(list);
				}
				return JsonDataResult.ok();
	}
}
